<?php
/**
 * 	JFormField Filelist Extend
 *
 * @package		SyntaxHighlighter
 * @subpackage	Parameter
 * @copyright		Copyright (C) 2007 - 2011 Yoshiki Kozaki(www.joomler.net) All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('JPATH_BASE') or die;

class JFormFieldFilelistExtend extends JFormField
{
	/**
	* Element name
	*
	* @access	protected
	* @var		string
	*/
	protected $type = 'FilelistExtend';

	protected function getInput()
	{
		jimport('joomla.filesystem.folder');
		jimport('joomla.filesystem.file');

		// path to images directory
		$path		= JPATH_ROOT.DS.$this->element->getAttribute('directory');
		$filter		= $this->element->getAttribute('filter');
		$exclude	= $this->element->getAttribute('exclude');
		$stripExt	= $this->element->getAttribute('stripext');
		$stripName = $this->element->getAttribute('stripname');
		$multiple = $this->element->getAttribute('multiple');
		$files		= JFolder::files($path, $filter);

		$options = array ();

		if (!$this->element->getAttribute('hide_none'))
		{
			$options[] = JHtml::_('select.option', '-1', JText::_('JOPTION_DO_NOT_USE'));
		}

		if (!$this->element->getAttribute('hide_default'))
		{
			$options[] = JHtml::_('select.option', '', JText::_('JOPTION_USE_DEFAULT'));
		}

		if (is_array($files))
		{
			foreach ($files as $file)
			{
				if ($exclude)
				{
					if (preg_match(chr(1) . $exclude . chr(1), $file))
					{
						continue;
					}
				}

				$filename = $file;

				if ($stripExt)
				{
					$filename = JFile::stripExt($file);
				}

				if($stripName)
				{
					$filename = str_replace($stripName, '', $filename);
				}
				$options[] = JHtml::_('select.option', $file, $filename);
			}
		}

		$attribs = array(
				'id' => $this->id,
				'list.attr' => 'class="inputbox"',
				'list.select' => $this->value
			);

		if($multiple){
			$this->multiple = true;
			$attribs['list.attr'] = $attribs['list.attr']. ' multiple="multiple" size="'. count($options). '"';
		}

		$fieldname = $this->getName($this->fieldname);

		return JHtml::_('select.genericlist', $options, $fieldname, $attribs);
	}
}
